﻿using System;
using System.Linq;
using Castle.Services.Transaction;
using Parker.Business;
using Rhino.Commons;
using WineCellar.Business.Entities;
using WineCellar.Core.Interfaces;

namespace Parker.Demo
{   
    internal class Program
    {
        private static void Main(string[] args)
        {            
            Console.WriteLine("Creating Windsor container");
            var container = new RhinoContainer("../../Windsor.boo");
            IoC.Initialize(container);
            Region region;
            User user;
            var parkerWineService = IoC.Resolve<IEntityService<Wine>>();
            var regionService = IoC.Resolve<IEntityService<Region>>();


            using (UnitOfWork.Start())
            {
                user = new User();
                user.UserName = "bart";
                user.Password = "bart";
                Repository<User>.Save(user);
                region = new Region();
                region.AddedBy = user;
                region.DateAdded = DateTime.Now;
                region.Name = "Bordeaux";
                regionService.Save(region);
            }
            using (UnitOfWork.Start())
            {
                var parkerWine = new ParkerWine();
                parkerWine.Region = region;
                parkerWine.AddedBy = user;
                parkerWine.Name = "Parker wine";
                parkerWine.Appelation = "Medoc";
                parkerWine.ColorType = "Red";
                parkerWine.DateAdded = DateTime.Now;
                parkerWine.Producer = "Lafitte";
                parkerWine.Score = 94;
                
                try
                {
                    parkerWineService.Save(parkerWine);   
                }                
                catch (InvalidOperationException)
                {
                    Console.WriteLine(String.Format("Could not save wine: {0} because the score {1} is too low",parkerWine.Name,parkerWine.Score));
                }
            }
            using (UnitOfWork.Start())
            {
                ParkerWine[] parkerWines = Repository<ParkerWine>.FindAll().ToArray();
                Console.WriteLine(string.Format("Parker wine found {0} - Score: {1}", parkerWines[0].Name,
                                                parkerWines[0].Score));
            }
            Console.ReadLine();
        }
    }
}